package com.wink_172.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.wink_172.commons.R
import com.wink_172.commons.databinding.ActivitySearchBarBinding
import com.wink_172.library.activity.SmartActivity
import com.wink_172.library.model.Constants
import com.wink_172.library.model.json.ImageData
import com.wink_172.library.utils.CommonUtils
import com.wink_172.library.view.SearchBarView
import org.xutils.image.ImageOptions
import org.xutils.x

/**
 * 搜索标题栏
 * Flow流布局使用
 */
class SearchBarActivity : SmartActivity() {

    lateinit var binding: ActivitySearchBarBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_search_bar)

        initView()
        init()

//        showProgressDialog(false)
    }

     fun init() {
        for (i in 0..9) {
            val itemview = LayoutInflater.from(this)
                .inflate(R.layout.item_view03, binding!!.flow1, false) as TextView
            itemview.text = "hello$i"
            itemview.setOnClickListener { }
            binding!!.flow1.addView(itemview)
        }
        for (i in 0..9) {
            val imageView = LayoutInflater.from(this)
                .inflate(R.layout.item_praise, binding!!.flow1, false) as ImageView
            val options = ImageOptions.Builder()
                .setUseMemCache(true)
                .setFadeIn(true)
                .setImageScaleType(ImageView.ScaleType.FIT_XY) //                    .setCircular(true)
                .build()
            val imageData = ImageData()
            //imageData.setResId(R.mipmap.ic_launcher_round);
            imageData.web_url = CommonUtils.randomImageUrl
            //if (!TextUtils.isEmpty(imageData.getWeb_url())) {
//                x.image().bind((ImageView) imageView, imageData.getWeb_url());
//
//            }else
//            if (!TextUtils.isEmpty(imageData.getLocal_url())) {
            x.image().bind(imageView, imageData.web_url)
            //
//            } else {
//                imageView.setImageResource(imageData.getResId());
//            }
            binding!!.flow1.addView(imageView)
        }
    }

    override fun onClick(v: View) {
        super.onClick(v)
        when (v.id) {
        }
    }

    private fun initView() {
        (binding!!.search as SearchBarView).setCallback(object : SearchBarView.ICallback {
            override fun onSendEvent(event: Int, content: String?) {
                when (event) {
                    Constants.EVENT_01 -> CommonUtils.showToast(content)
                    Constants.EVENT_02 -> finish()
                }
            }
        })
    }

    companion object {
        fun startActivity(context: Context, vararg args: Any?) {
            val intent = Intent(context, SearchBarActivity::class.java)

            //intent.putExtra(Constants.PARAM_DATA1, (String) args[0]);
            context.startActivity(intent)
        }

        private const val TAG = "SearchBarActivity"
    }
}